

from rest_framework import serializers
from goods.models import SKU
from orders.models import OrderInfo,OrderGoods

# 嵌套SKU序列化器
class SKUSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = SKU
        fields = [
            'name',
            'default_image'
        ]

# 嵌套OrderGoods序列化器
class OrderGoodsSerializer(serializers.ModelSerializer):
    # sku是当前OrderGoods对象关联的单一的主表SKU对象
    sku = SKUSimpleSerializer()

    class Meta:
        model = OrderGoods
        fields = [
            'count',
            'price',
            'sku' # 当前OrderGoods对象关联的SKU(主)对象
        ]

# 订单详情序列化器
class OrderInfoDetailModelSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField()

    # 隐藏字段skus记录当前订单OrderInfo关联的从表OrderGoods多个对象数据
    skus = OrderGoodsSerializer(many=True)

    class Meta:
        model = OrderInfo
        fields = "__all__"



# 订单列表序列化器
class OrderInfoSimpleModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = OrderInfo
        fields = [
            'order_id',
            'create_time'
        ]